<?php 
class ModulesController extends Controller
{
	public function addAction()
	{
		$this->save();
		$this->view->prefix = CDbConfig::getPrefix();
		
	}
	
	public function editAction()
	{
		$this->save();
	}
	
	public function listAction()
	{
		$do = $this->request->getPost('do');
		if (null !== $do)
		{
			$model = $this->loadModel();
			$m = '';
			if (strcmp($do, 'status') === 0)
			{
				$model->module_status = $model->module_status == 'y' ? 'n' : 'y';
				$m = '已成功保存对该模型所作的修改：' . $model->module_name;
				$model->save();
			}
			else if (strcmp($do, 'delete') === 0)
			{
				$m = '已成功删除一个模型：' . $model->module_name;
				$model->delete();
			}
			
			$this->forward(array('m' => $m));
		}
		
		$models = ModulesModel::model()->findAll();
		$this->view->models = $models;
	}
	
	protected function save()
	{	
		$model = CControllerConfig::getAction() == 'edit' ? $this->loadModel() : new ModulesModel();
		
		if ($attributes = $this->request->getPost('modules'))
		{
			$model->attributes = $attributes;
			if ($model->save())
			{
				$params = array(
					'id' => $model->pk,
					'm' => '已成功保存对该模型所作的修改：' . $model->module_name, 
					't' => self::T_MESSAGE
				);
				$do = $this->request->getPost('do', 'save');
				if (strcmp($do, 'save') === 0)
					$this->forward($params, 'list');
				else 
					$this->forward($params, 'edit');
			}
			else 
			{
				$_GET['m'] = '无法保存该模型的信息：' . $model->getError();
				$_GET['t'] = self::T_ERROR;
			}
		}
		
		$this->view->model = $model;
	}
}
